﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BonjourHotel.Core.Localization;
using BonjourHotel.Core.Controls;
using BonjourHotel.Core.Hotels;
using BonjourHotel.Model;
using BonjourHotel.Core.Countries;
using BonjourHotel.Core.Counties;
using BonjourHotel.Core.Utils;
using System.IO;

namespace BonjourHotel.Application.Controls.Hotels
{
    public partial class HotelCreation : BaseUserControl
    {
        /// <summary>
        /// id used to edit an existing hotel
        /// </summary>
        private int idHotel
        {
            get
            {
                return CoreUtils.QueryStringInt("idHotel", 0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApplyLocalization();

                if (idHotel != 0)
                {
                    BindData();
                }
            }
        }

        protected void ApplyLocalization()
        {
            lblHotelName.Text = LocaleManager.GetLocaleString("controls.hotel.creation.HotelName");
            lblVatCode.Text = LocaleManager.GetLocaleString("controls.hotel.creation.VatCode");
            lblAddress.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Address");
            lblZipCode.Text = LocaleManager.GetLocaleString("controls.hotel.creation.ZipCode");
            lblCountry.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Country");
            lblCounty.Text = LocaleManager.GetLocaleString("controls.hotel.creation.County");
            lblPhone.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Phone");
            lblFax.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Fax");
            lblEmail.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Email");
            lblWebsite.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Website");
            lblHotelType.Text = LocaleManager.GetLocaleString("controls.hotel.creation.HotelType");
            //lblLogo.Text = LocaleManager.GetLocaleString("controls.hotel.creation.Logo");
            ddCountry.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropdownItem"), "0"));
            ddCounty.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropdownItem"), "0"));
            ddHotelType.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropDownItem"), "0"));
            btnSave.Text = LocaleManager.GetLocaleString("controls.common.save");

            //Populate country combo
            CountryFacade countryFacade = new CountryFacade();
            ddCountry.DataSource = countryFacade.GetAllCountries();
            ddCountry.DataTextField = "Name";
            ddCountry.DataValueField = "idCountry";
            ddCountry.DataBind();

            //Populate hotel types combo
            HotelTypeFacade hotelTypeFacade = new HotelTypeFacade();
            ddHotelType.DataSource = hotelTypeFacade.GetAllTypes();
            ddHotelType.DataTextField = "Description";
            ddHotelType.DataValueField = "idHotelType";
            ddHotelType.DataBind();
        }
        protected void ddCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            PopulateCounties();
        }
        private void PopulateCounties()
        {
            int idCountry = Int32.Parse(ddCountry.SelectedValue);
            CountyFacade countyFacade = new CountyFacade();
            ddCounty.DataSource = countyFacade.GetAllCounties(idCountry);
            ddCounty.DataTextField = "Name";
            ddCounty.DataValueField = "idCounty";
            ddCounty.DataBind();

        }

        private void BindData()
        {
            HotelFacade hotelFacade = new HotelFacade();
            Hotel hotel = hotelFacade.GetHotel(idHotel);

            if (hotel != null)
            {
                tbHotelName.Text = hotel.Name;
                tbVatCode.Text = hotel.VatCode;
                tbAddress.Text = hotel.Address;
                tbZipCode.Text = hotel.ZipCode;
                tbPhone.Text = hotel.PhoneNumber;
                tbFax.Text = hotel.FaxNumber;
                tbEmail.Text = hotel.EmailAddress;
                tbWebsite.Text = hotel.WebSite;

                if (!hotel.CountryReference.IsLoaded) hotel.CountryReference.Load();
                if (hotel.Country != null)
                {
                    CoreUtils.SelectListItem(ddCountry, hotel.Country.idCountry);
                    PopulateCounties();
                }

                if (!hotel.CountyReference.IsLoaded) hotel.CountyReference.Load();
                if (hotel.County != null)
                {
                    CoreUtils.SelectListItem(ddCounty, hotel.County.idCounty);
                }

                if (!hotel.HotelTypeReference.IsLoaded) hotel.HotelTypeReference.Load();
                if (hotel.HotelType != null)
                {
                    CoreUtils.SelectListItem(ddHotelType, hotel.HotelType.idHotelType);
                }

                //if (!String.IsNullOrEmpty(hotel.LogoImagePath))
                //    uplLogo.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HotelFacade hotelFacade = new HotelFacade();
                Hotel newHotel = null;

                if (idHotel != 0)
                    newHotel = hotelFacade.GetHotel(idHotel);
                else
                {
                    newHotel = new Hotel();
                    hotelFacade.Add(newHotel);
                }

                newHotel.Name = tbHotelName.Text;
                newHotel.VatCode = tbVatCode.Text;
                newHotel.Address = tbAddress.Text;
                newHotel.ZipCode = tbZipCode.Text;
                if (ddCountry.SelectedValue != "0")
                {
                    int idCountry = Int32.Parse(ddCountry.SelectedValue);
                    newHotel.CountryReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.Countries", "idCountry", idCountry);
                }
                else
                {
                    newHotel.Country = null;
                }

                if (ddCounty.SelectedValue != "0")
                {
                    int idCounty = Int32.Parse(ddCounty.SelectedValue);
                    newHotel.CountyReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.Counties", "idCounty", idCounty);
                }
                else
                {
                    newHotel.County = null;
                }

                newHotel.PhoneNumber = tbPhone.Text;
                newHotel.FaxNumber = tbFax.Text;
                newHotel.EmailAddress = tbEmail.Text;
                newHotel.WebSite = tbWebsite.Text;

                if (ddHotelType.SelectedValue != "0")
                {
                    int idHotelType = Int32.Parse(ddHotelType.SelectedValue);
                    newHotel.HotelTypeReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.HotelTypes", "idHotelType", idHotelType);
                }
                
                //newHotel.LogoImagePath = uplLogo.FileName;
                hotelFacade.SaveAllObjectChanges();
            }
        }

        //protected void uplLogo_Upload(object sender, EventArgs e)
        //{
        //    string path = "~/Application/Public/Images/";
        //    string fullPath = Server.MapPath(path);
        //    if (!Directory.Exists(fullPath))
        //        Directory.CreateDirectory(fullPath);

        //    uplLogo.SaveAs(Path.Combine(fullPath, uplLogo.FileName));
                
        //}

    }
}